package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import liumengbaio.lolzhangyoubao.Activity.InFo.Lolinfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.UserInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/8/29.
 */
public class HeadIcon extends AppCompatActivity implements View.OnClickListener {
    private Button head_left_title, head_right_title;
    private CircleImageView head_icon;
    private TextView head_title, zhangyoubao_ID, stars, user_ID, Area, signature;
    private UserInfo user_information;
    private ImageView user_image;
    private boolean flag;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String url = "";

    private Lolinfo lolinfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_icon);
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        editor = sp.edit();
        initView();
        getDataFromWeb();
        initData();
    }

    private void initView() {
        url = getIntent().getExtras().getString("url", "");
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        head_left_title = (Button) findViewById(R.id.head_left_title);
        head_right_title = (Button) findViewById(R.id.head_right_title);
        user_ID = (TextView) findViewById(R.id.user_ID);
        head_icon = (CircleImageView) findViewById(R.id.head_icon);
        head_title = (TextView) findViewById(R.id.head_title);
        user_image = (ImageView) findViewById(R.id.user_image);
        zhangyoubao_ID = (TextView) findViewById(R.id.zhangyoubao_ID);
        signature = (TextView) findViewById(R.id.signature);
        stars = (TextView) findViewById(R.id.stars);
        Area = (TextView) findViewById(R.id.Area);
    }

    private void initData() {
        user_image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Picasso.with(HeadIcon.this).load("http://cdn.tgp.qq.com/pallas/images/usericon/1234.png").into(user_image);
        head_title.setText(sp.getString("name", "游客登录"));
        user_ID.setText(sp.getString("summoner", "未找到该用户"));
        Area.setText(sp.getString("area", "未知大区"));
        signature.setText(sp.getString("signature", ""));
        Picasso.with(HeadIcon.this).load(sp.getString("headIcon", "")).into(head_icon);
        head_left_title.setOnClickListener(this);
        head_right_title.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.head_left_title:
                finish();
                break;
            case R.id.head_right_title:
                Intent intent_right_title = new Intent(HeadIcon.this, EditActivity.class);
                startActivity(intent_right_title);
                break;
        }

    }

    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(HeadIcon.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        lolinfo = gson.fromJson(result, Lolinfo.class);
                        if (lolinfo != null) {
                            handler.sendEmptyMessage(1);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                stars.setText(lolinfo.getData().getConstellation() + lolinfo.getData().getCity());
                editor.putString("icon", lolinfo.getData().getUser_lol_info().getIcon());
                editor.putString("area", lolinfo.getData().getUser_lol_info().getArea());
                editor.putString("field", lolinfo.getData().getUser_lol_info().getField());
                editor.putString("signature", lolinfo.getData().getSignature());
                editor.putString("good_at", lolinfo.getData().getUser_lol_info().getGood_at());
                editor.putString("summoner", lolinfo.getData().getUser_lol_info().getSummoner());
                editor.putString("city", lolinfo.getData().getCity());
                editor.commit();
            }
        }
    };
}
